<template>
    <div class="f-box">
        <div class="header">
            <label>创建用户</label>
        </div>
        <div class="form">
            <el-form ref="form" label-width="100px" label-position="left">
                <el-form-item label="用户名">
                    <el-input v-model="userName"></el-input>
                </el-form-item>
                <el-form-item label="昵称">
                    <el-input v-model="nickName"></el-input>
                </el-form-item>
                <el-form-item label="密码">
                    <el-input v-model="passWord"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSubmit">提交</el-button>
                    <el-button @click="back">返回</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>
<script>
import request from "@/utils/request";
import { formatDate } from "@/utils/format";
export default {
  name: "createUser",
  data() {
    return {
      userName: "",
      nickName: "",
      passWord: "",
      createDate: "",
      userId: ""
    };
  },
  methods: {
    uuid() {
      return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
        (c ^
          (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)))
        ).toString(16)
      );
    },
    onSubmit() {
      this.createDate = formatDate(new Date());
      this.userId = this.uuid();
      const params = {
        userId: this.userId,
        userName: this.userName,
        nickName: this.nickName,
        passWord: this.passWord,
        createDate: this.createDate
      };
      request({
        url: "/api/user/createUser",
        method: "post",
        data: params
      }).then(res => {
        console.log(res);
        if (res.msg === "新增成功") {
          this.$message({
            message: "恭喜你，新增成功",
            type: "success"
          });
          this.$router.push("/user");
        }
      });
    },
    back() {
      this.$router.push("/user");
    }
  }
};
</script>
<style lang="scss" scoped>
.f-box {
  width: 100%;
  height: 700px;
  //   border: 1px solid #fff;
  margin: auto;
  .header {
    width: 50%;
    margin: auto;
    height: 50px;
    background: rgb(68, 114, 196);
    line-height: 50px;
    color: #fff;
    text-align: center;
    label {
      font-size: 16px;
    }
  }
  .form {
    width: 50%;

    margin: auto;
    margin-top: 20px;
  }
}
</style>
